package com.bw.msg.mvvm.viewmodel;

import com.blankj.utilcode.util.LogUtils;
import com.bw.msg.adapter.MsgAdapter;
import com.bw.msg.mvvm.model.Msgmodel;
import com.bw.msg.mvvm.model.entity.MsgEntity;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.dao.UserBeanDao;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/19 0019 上午 8:57
 *
 */
class Msgviewmodel extends BaseViewModel<Msgmodel> {
    public BaseQuickAdapter adapter = new MsgAdapter();
    @Override
    public void onCreate() {
        super.onCreate();
        requestMsg();

    }
    public void requestMsg(){
        List<UserBean> list = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list();

        if (list.isEmpty()){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",0);
            m.requestmsg(map).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }else {
            int user_id = list.get(0).getUser_id();
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",user_id);
            m.requestmsg(map).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }


    }


    @Override
    protected void result(BaseEntity entity) {
        MsgEntity msgEntity = (MsgEntity) entity;
        List<MsgEntity.DataBean> data = msgEntity.getData();
        adapter.setNewInstance(data);
        LogUtils.e("mmmmmmmmmmm"+data);
    }
}
